package com.fudan.adse.controller;

import com.fudan.adse.api.CommonPage;
import com.fudan.adse.api.CommonResult;
import com.fudan.adse.domain.cont.CheckStatus;
import com.fudan.adse.domain.cont.OrderStatus;
import com.fudan.adse.domain.dto.NewOrderDTO;
import com.fudan.adse.domain.po.TicketOrder;
import com.fudan.adse.domain.po.Wallet;
import com.fudan.adse.domain.vo.StatisticsVO;
import com.fudan.adse.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;

/**
 * 购票管理控制器
 *
 * @author Jerry Zhang
 * create: 2022-11-23 22:50
 */
@RestController
@RequestMapping("/api/v1/oms")
public class OrderController {
    @Autowired
    private OrderService orderService;

    /**
     * 新增订单
     *
     * @param newOrder
     * @return
     */
    @PostMapping("/outer/order")
    public CommonResult<TicketOrder> addOrder(@RequestBody NewOrderDTO newOrder) {
        return CommonResult.success(orderService.addOrder(newOrder));
    }

    /**
     * 支付订单
     *
     * @param uuid
     * @return
     */
    @PutMapping("/outer/order/paid")
    public CommonResult<Wallet> paid(@NotBlank @RequestParam("uuid") String uuid) {
        return CommonResult.success(orderService.updateOrder(uuid, OrderStatus.PAID.getStatus(), null));
    }

    /**
     * 取消订单
     *
     * @param uuid
     * @return
     */
    @PutMapping(value = "/outer/order/refund")
    public CommonResult<Wallet> refund(@RequestParam("uuid") String uuid) {
        return CommonResult.success(orderService.updateOrder(uuid, null, CheckStatus.REFUND.getStatus()));
    }

    /**
     * 分页获取购票订单
     *
     * @param username
     * @param status      订单状态：未支付、已支付、已取消
     * @param checkStatus 影票使用状态：已使用、待使用、已退票
     * @return
     */
    @GetMapping("/outer/orders")
    public CommonResult<CommonPage<TicketOrder>> getOrdersByUsername(@RequestParam(value = "username", required = false) String username,
                                                                     @RequestParam(value = "status", required = false) String status,
                                                                     @RequestParam(value = "checkStatus", required = false) String checkStatus,
                                                                     @RequestParam(value = "rowPieceId", required = false) Integer rowPieceId,
                                                                     @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                                                                     @RequestParam(value = "pageSize", required = false, defaultValue = "15") Integer pageSize) {
        return CommonResult.success(CommonPage.restPage(orderService.getOrdersByConditions(username, status, checkStatus, rowPieceId, pageNum, pageSize)));
    }


    /**
     * 使用影票
     *
     * @param uuid
     * @return
     */
    @PutMapping("/order/used")
    public CommonResult used(@NotBlank @RequestParam("uuid") String uuid) {
        orderService.updateOrder(uuid, null, CheckStatus.CHECKED.getStatus());
        return CommonResult.success(null);
    }


    /**
     * 分级查看影院、影片以及场次的收益情况
     *
     * @param cinemaId
     * @param filmId
     * @param rowPieceId
     * @return
     */
    @GetMapping("/statistics")
    public CommonResult<StatisticsVO> getStatisticsDataByConditions(
            @RequestParam(value = "cinemaId", required = false) Integer cinemaId,
            @RequestParam(value = "filmId", required = false) Integer filmId,
            @RequestParam(value = "rowPieceId", required = false) Integer rowPieceId) {
        return CommonResult.success(orderService.getStatisticsDataByConditions(cinemaId, filmId, rowPieceId));
    }
}
